//-----------------------------------------------------------------------------
// SimplePull
//
// Copyright (c) MetaSwitch 2006
// Author: Alex Davies
//
// Sample application demonstrating the SOAP Sh interface to the MetaView
// Server.  This application sends in an Sh-Pull request for part of an object's
// configuration and prints out the result.  It uses Apache Axis and the
// "untyped" WSDL file.
//
// See the readme file for more information.
//-----------------------------------------------------------------------------

import javax.xml.rpc.holders.*;

import org.apache.axis.message.MessageElement;

import com.MetaSwitch.EMS.SOAP.*;
import com.MetaSwitch.EMS.SOAP.holders.*;

public class SimplePull
{
  private static final String USAGE = "Usage: SimplePull " +
                                      "<User identity> [<Service " +
                                      "indication(s)>]";
  private static ShUntypedUtilities utilities = new ShUntypedUtilities();

  public static void main(String[] args)
  {
    StringHolder servIndHolder = new StringHolder();
    StringHolder uIHolder = new StringHolder();

    IntHolder resultCodeHolder = new IntHolder();
    TExtendedResultHolder extendedResultHolder = new TExtendedResultHolder();
    TUserDataHolder userDataHolder = new TUserDataHolder();

    try
    {
      //-----------------------------------------------------------------------
      // Extract information from the command line arguments.
      //-----------------------------------------------------------------------
      getParameters(args, uIHolder, servIndHolder);

      //-----------------------------------------------------------------------
      // Connect to the Sh service and send in a Pull request.
      //-----------------------------------------------------------------------
      ShServiceLocator locator = new ShServiceLocator();

      Sh shInstance = locator.getShService();

      shInstance.shPull(uIHolder.value,
                        0,
                        servIndHolder.value,
                        utilities.ORIGIN_HOST,
                        resultCodeHolder,
                        extendedResultHolder,
                        userDataHolder);

      //-----------------------------------------------------------------------
      // Check whether the request succeeded and display the value of each of
      // the fields.
      //-----------------------------------------------------------------------
      utilities.checkResultCode(resultCodeHolder.value,
                                extendedResultHolder.value,
                                userDataHolder.value);

      utilities.displayFields(userDataHolder.value);
    }
    catch (MetaSwitchShInterfaceException e)
    {
      System.err.println(e.getMessage());
      System.err.println(USAGE);
    }
    catch (org.apache.axis.AxisFault e)
    {
      System.err.println("Problem connecting to MetaView server: " + e);
      System.err.println(USAGE);
    }
    catch (javax.xml.rpc.ServiceException e)
    {
      System.err.println("Could not connect to Sh service");
      System.err.println(USAGE);
    }
    catch (Exception e)
    {
      System.err.println("Unexpected error \"" + e
                         + "\" in retrieving data");
      e.printStackTrace(System.err);
      System.err.println(USAGE);
    }
  }

  /**
   * Parse the command line arguments and extract the necessary information.
   *
   * The first parameter is the subscriber's DN.  The second is the service
   * indication and is optional; it defaults to "BaseInformation" if not
   * specified.
   *
   * @param args        IN  The arguments provided at the command line.
   * @param uI          OUT The user identity to request.
   * @param servInd     OUT The service indication to request.
   *
   * @exception WrongParametersException
   */
  private static void getParameters(String[] args,
                                    StringHolder uI,
                                    StringHolder servInd)
    throws WrongParametersException
  {
    if ((args.length < 1) || (args.length > 2))
    {
      throw new WrongParametersException("The wrong number of parameters was" +
                                         " provided");
    }

    uI.value = args[0];
    servInd.value =
              (args.length == 2 ? args[1] : "Meta_Subscriber_BaseInformation");
  }
}
